package com.hxl.tech.gateway.auth.dao;


import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.hxl.tech.gateway.auth.context.ContextHolder;
import com.hxl.tech.gateway.auth.dto.UserInfo;
import com.hxl.tech.gateway.auth.dto.user.UserFindDTO;
import com.hxl.tech.gateway.auth.model.QUser;
import com.hxl.tech.gateway.auth.model.User;
import com.hxl.tech.gateway.auth.util.QueryDslOrderUtil;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Repository
public class UserDao {

    @Resource
    private JPAQueryFactory jpaQueryFactory;

    private QUser qUser = QUser.user;

    public List<User> find(UserFindDTO findDTO, Boolean page) {

        JPAQuery<User> query = jpaQueryFactory.select(qUser).from(qUser);
        query.where(booleanBuilder(findDTO));

        if (Objects.nonNull(findDTO.getProperty()) && Objects.nonNull(findDTO.getDirection())) {
            OrderSpecifier orderSpecifier = QueryDslOrderUtil.orderByField(qUser, findDTO.getDirection(), findDTO.getProperty());
            query.orderBy(orderSpecifier);
        } else {
            query.orderBy(qUser.createTime.desc());
        }

        return BooleanUtil.isTrue(page) ? query.limit(findDTO.limit()).offset(findDTO.offset()).fetch() : query.fetch();
    }

    public Long count(UserFindDTO findDTO) {

        JPAQuery<String> query = jpaQueryFactory.select(qUser.userId).from(qUser);
        query.where(booleanBuilder(findDTO));
        return Long.valueOf(query.fetch().size());
    }

    public BooleanBuilder booleanBuilder(UserFindDTO findDTO) {

        UserInfo userInfo = ContextHolder.getUserInfo();
        BooleanBuilder builder = new BooleanBuilder();
        if (StringUtils.isNotEmpty(findDTO.getUserId())) {
            builder.and(qUser.userId.eq(findDTO.getUserId()));
        }
        if(!userInfo.getIsAdmin()) {
            builder.and(qUser.userId.eq(userInfo.getUserId()));
        }
        if (StrUtil.isNotBlank(findDTO.getRoleName())) {
            builder.and(qUser.roleName.eq(findDTO.getRoleName()));
        }
        if (Objects.nonNull(findDTO.getState())) {
            builder.and(qUser.state.eq(findDTO.getState()));
        }
        if (StrUtil.isNotBlank(findDTO.getUserName())) {
            builder.and(qUser.userName.like("%" + findDTO.getUserName() + "%"));
        }
        if (StrUtil.isNotBlank(findDTO.getPhone())) {
            builder.and(qUser.phone.like("%" + findDTO.getPhone() + "%"));
        }
        return builder;
    }

    public User read(String userId) {

        JPAQuery<User> query = jpaQueryFactory.select(qUser).from(qUser).where(qUser.userId.eq(userId));
        return query.fetchOne();
    }

    public User readByPhone(String phone) {

        JPAQuery<User> query = jpaQueryFactory.select(qUser).from(qUser).where(qUser.phone.eq(phone));
        return query.fetchOne();
    }
}
